<script setup lang="ts">
import useUserStore from '@/store/user'
import { getCode, dateFormat, toast } from '@/common/util'
import { getAd, getQuestRewards } from '@/common/api'
import type { AnyObject } from '@/common/types'
const userStore = useUserStore()

async function handleGetAd() {
  const res = await getAd()
  userStore.adUnitId = res.data
}

onLaunch(async(e?: AnyObject) => {
  console.log('App Launch')
  tt.disableUserScreenRecord()
  if (!userStore.userId) {
    const code = await getCode()
    await userStore.userLogin(code, e?.query)
  }
  await handleGetAd()
})
onShow((e?: AnyObject) => {
  console.log('App Show')
  if (e?.scene === '021003' || e?.scene === '021020') {
    const today = dateFormat()
    const history = uni.getStorageSync('orange-music-theatre-get-open-' + e.scene)
    if (today !== history) handleGetQuestRewards(e.scene)
  }
})
onHide(() => {
  console.log('App Hide')
})

async function handleGetQuestRewards(scene: string) {
  const id = scene === '021003' ? 1 : 4
  const toastText = scene === '021003' ? '我的收藏' : '桌面'
  await getQuestRewards(id)
  toast(`从${toastText}打开，送20K币`)
  uni.setStorageSync('orange-music-theatre-get-open-' + scene, dateFormat())
}

</script>
<style lang="scss">
@import "./style/globe.scss";
@import "./style/iconfont.scss";
</style>
